package cn.lijiguo.concurrency.part1.ch06;

import java.util.concurrent.TimeUnit;

/**
 * CantInterrupt
 * 如果 while 里面放 try catch 会导致 中断失效
 * 原因 响应中段之后 sleep 会把 中断标记位清除
 * @author lijiguo
 * @date 2020/1/6 11:12
 */
public class CantInterrupt {

    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = () -> {
            int num = 0;
            while (num <= 10000 && !Thread.currentThread().isInterrupted()) {
                if (num % 100 ==0) {
                    System.out.println(num + " 是 100 的倍数");
                }
                num++;

                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        Thread t1 = new Thread(runnable, "t1");
        t1.start();

        TimeUnit.SECONDS.sleep(5);

        t1.interrupt();
    }
}
